﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameEngine;
using EngineTest2CLR;

namespace EngineTests2
{
    /// <summary>
    /// English checkers test
    /// </summary>
    public class EngineTest1
    {
        private GameManager _mng;
        private CLRBitBoard bb;

        public EngineTest1()
        {
            _mng = GameManagerFactory.CreateGameManager(enumGameType.English);
            _mng.NewGame();

            bb = BitBoardConverter.ConvertGameBoard(_mng.GameBoard);
        }

        public void PlayGame()
        {
            // initialization
            CLREngineWrapper wrapper = new CLREngineWrapper();
            int logId = wrapper.LogInit("c:\\Log\\EnglishBB.log");
            Search search = new Search(wrapper, logId);

            // do moves
            try
            {
                bool bWhite = true;
                for (int i = 0; i < 200; i++)
                {
                    Move bestMove;
                    search.Negamax(_mng, bb, 7, bWhite, out bestMove);
                    if (bestMove == null)
                        break;

                    CLRBitBoard bbMove = BitBoardConverter.ConvertMoveToBB(
                            bestMove, _mng.GameBoard.GetField(bestMove.startPos), _mng.GameBoard.Size / 2);

                    // log move
                    wrapper.LogMessage(logId, string.Format("Move {0}\n", i));
                    wrapper.LogBB(logId, bb);
                    wrapper.LogBB(logId, bbMove);
                    Console.WriteLine(string.Format("Move {0}", i));

                    // do move
                    _mng.DoMove(bestMove);
                    bb.wm ^= bbMove.wm;
                    bb.bm ^= bbMove.bm;
                    bb.wk ^= bbMove.wk;
                    bb.bk ^= bbMove.bk;

                    bWhite = !bWhite;
                } // for
            }
            catch (ApplicationException )
            {
                
            }
            finally
            {
                wrapper.LogClose(logId);
            }
        }

    }
}
